package com.yicong.ants.utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;

import com.yicong.ants.R;

/**
 * 跳转页面工具类
 */
public class IntentUtils {
    /**
     * 错误的 TAG
     */
    private static final int ERROR_TAG = -123;

    public static void startActivity(Intent intent, Activity activity) {
        startActivityForResult(intent, activity, null, ERROR_TAG);
    }

    public static void startActivity(Intent intent, Activity activity, Bundle bundle) {
        startActivityForResult(intent, activity, bundle, ERROR_TAG);
    }

    public static void startActivity(Intent intent, Activity activity, int tag) {
        startActivityForResult(intent, activity, null, tag);
    }

    public static void startActivityForResult(Intent intent, Activity activity, Bundle bundle, int tag) {
        if (null == intent || null == activity) {
            return;
        }
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        if (tag != ERROR_TAG) {
            activity.startActivityForResult(intent, tag);
        } else {
            activity.startActivity(intent);
        }
        initAnim(activity);
    }

    public static void startActivity(Intent intent, Fragment fragment) {
        startActivityForResult(intent, fragment, null, ERROR_TAG);
    }

    public static void startActivity(Intent intent, Fragment fragment, Bundle bundle) {
        startActivityForResult(intent, fragment, bundle, ERROR_TAG);
    }

    public static void startActivity(Intent intent, Fragment fragment, int tag) {
        startActivityForResult(intent, fragment, null, tag);
    }

    public static void startActivityForResult(Intent intent, Fragment fragment, Bundle bundle, int tag) {
        if (null == intent || null == fragment) {
            return;
        }
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        if (tag != ERROR_TAG) {
            fragment.startActivityForResult(intent, tag);
        } else {
            fragment.startActivity(intent);
        }
        initAnim(fragment.getActivity());
    }

    /**
     * 页面进入动画
     *
     * @param activity
     */
    private static void initAnim(Activity activity) {
        if (null != activity) {
            activity.overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
        }
    }

    /**
     * 页面退出动画
     *
     * @param activity
     */
    public static void exitAnim(Activity activity) {
        if (null != activity) {
            activity.overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
        }
    }
}
